home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / adatutor / lrmrdr / chap10.doc < prev    next >
Text File  |  1996-01-30  |  32KB  |  786 lines

  1. >                10. Program Structure and Compilation Issues
  2.  
  3.  
  4. The   overall  structure  of  programs  and  the  facilities  for  separate
  5. compilation are described in this chapter.  A program is  a  collection  of
  6. one  or  more  compilation  units  submitted  to  a compiler in one or more
  7. compilations.  Each compilation unit specifies the separate compilation  of
  8. a  construct  which  can  be  a  subprogram  declaration or body, a package
  9. declaration  or  body,  a  generic  declaration  or  body,  or  a   generic
  10. instantiation.   Alternatively  this  construct  can be a subunit, in which
  11. case it includes the body of a subprogram, package, task unit,  or  generic
  12. unit declared within another compilation unit.
  13.  
  14.  
  15. References:   compilation  10.1,  compilation unit 10.1, generic body 12.2,
  16. generic declaration 12.1, generic instantiation  12.3,  package  body  7.1,
  17. package  declaration  7.1, subprogram body 6.3, subprogram declaration 6.1,
  18. subunit 10.2, task body 9.1, task unit 9
  19.  
  20. > 10.1  Compilation Units - Library Units
  21.  
  22.  
  23. The text of a program can be submitted to  the  compiler  in  one  or  more
  24. compilations.  Each compilation is a succession of compilation units.
  25.  
  26.  
  27.     compilation ::= {compilation_unit}
  28.  
  29.     compilation_unit ::=
  30.          context_clause library_unit | context_clause secondary_unit
  31.  
  32.     library_unit ::=
  33.          subprogram_declaration | package_declaration
  34.        | generic_declaration    | generic_instantiation
  35.        | subprogram_body
  36.  
  37.     secondary_unit ::= library_unit_body | subunit
  38.  
  39.     library_unit_body ::= subprogram_body | package_body
  40.  
  41.  
  42. The compilation units of a program are said to belong to a program library.
  43. A  compilation  unit  defines either a library unit or a secondary unit.  A
  44. secondary unit is either the separately compiled proper body of  a  library
  45. unit,  or  a  subunit  of  another  compilation  unit.  The designator of a
  46. separately compiled subprogram (whether a library unit or a  subunit)  must
  47. be an identifier.  Within a program library the simple names of all library
  48. units must be distinct identifiers.
  49.  
  50.  
  51. The effect of compiling a library unit is to define (or redefine) this unit
  52. as one that belongs to the program library.  For the visibility rules, each
  53. library  unit  acts  as  a  declaration  that occurs immediately within the
  54. package STANDARD.
  55.  
  56.  
  57. The effect of compiling a secondary unit is to define the body of a library
  58. unit, or in the case of a subunit, to define the proper body of  a  program
  59. unit that is declared within another compilation unit.
  60.  
  61.  
  62. A subprogram body given in a compilation unit is interpreted as a secondary
  63. unit  if  the  program  library  already  contains a library unit that is a
  64. subprogram with the same name;  it  is  otherwise  interpreted  both  as  a
  65. library  unit  and  as  the  corresponding library unit body (that is, as a
  66. secondary unit).
  67.  
  68.  
  69. The compilation units of a compilation are compiled in the given order.   A
  70. pragma  that  applies  to the whole of a compilation must appear before the
  71. first compilation unit of that compilation.
  72.  
  73.  
  74. A subprogram that is a library unit can be used as a main  program  in  the
  75. usual sense.  Each main program acts as if called by some environment task;
  76. the  means  by  which this execution is initiated are not prescribed by the
  77. language definition.  An implementation may impose certain requirements  on
  78. the  parameters  and  on  the  result,  if  any,  of  a main program (these
  79. requirements  must  be  stated  in  Appendix  F).   In  any   case,   every
  80. implementation  is  required  to  allow,  at  least, main programs that are
  81. parameterless procedures, and every main program must be a subprogram  that
  82. is a library unit.
  83.  
  84. Notes:
  85.  
  86.  
  87. A  simple  program may consist of a single compilation unit.  A compilation
  88. need not have any compilation units;  for example, its text can consist  of
  89. pragmas.
  90.  
  91.  
  92. The  designator  of  a library function cannot be an operator symbol, but a
  93. renaming declaration  is  allowed  to  rename  a  library  function  as  an
  94. operator.   Two  library  subprograms  must  have distinct simple names and
  95. hence cannot overload  each  other.   However,  renaming  declarations  are
  96. allowed  to  define  overloaded  names  for such subprograms, and a locally
  97. declared subprogram is allowed  to  overload  a  library  subprogram.   The
  98. expanded  name STANDARD.L can be used for a library unit L (unless the name
  99. STANDARD is hidden) since library units  act  as  declarations  that  occur
  100. immediately within the package STANDARD.
  101.  
  102.  
  103. References:   allow 1.6, context clause 10.1.1, declaration 3.1, designator
  104. 6.1, environment 10.4,  generic  declaration  12.1,  generic  instantiation
  105. 12.3, hiding 8.3, identifier 2.3, library unit 10.5, local declaration 8.1,
  106. must  1.6,  name  4.1, occur immediately within 8.1, operator 4.5, operator
  107. symbol 6.1, overloading 6.6 8.7, package body 7.1, package declaration 7.1,
  108. parameter of a subprogram 6.2, pragma 2.8, procedure 6.1, program  unit  6,
  109. proper  body  3.9,  renaming  declaration  8.5,  simple  name 4.1, standard
  110. package 8.6, subprogram 6, subprogram body 6.3, subprogram declaration 6.1,
  111. subunit 10.2, task 9, visibility 8.3
  112.  
  113. > 10.1.1  Context Clauses - With Clauses
  114.  
  115.  
  116. A context clause is used to specify  the  library  units  whose  names  are
  117. needed within a compilation unit.
  118.  
  119.  
  120.     context_clause ::= {with_clause {use_clause}}
  121.  
  122.     with_clause ::= with unit_simple_name {, unit_simple_name};
  123.  
  124.  
  125. The  names  that  appear  in  a  context clause must be the simple names of
  126. library units.  The simple name of any library unit  is  allowed  within  a
  127. with  clause.   The  only names allowed in a use clause of a context clause
  128. are the simple names of library packages mentioned by previous with clauses
  129. of the context clause.  A simple name declared by a renaming declaration is
  130. not allowed in a context clause.
  131.  
  132.  
  133. The with clauses and use clauses of the context clause of  a  library  unit
  134. apply  to this library unit and also to the secondary unit that defines the
  135. corresponding body (whether such a clause  is  repeated  or  not  for  this
  136. unit).   Similarly,  the with clauses and use clauses of the context clause
  137. of a compilation unit apply to this unit and also to its subunits, if  any.
  138.  
  139.  
  140. If  a  library unit is named by a with clause that applies to a compilation
  141. unit, then this library unit is directly  visible  within  the  compilation
  142. unit,  except  where  hidden;   the  library unit is visible as if declared
  143. immediately within the package STANDARD (see 8.6).
  144.  
  145.  
  146. Dependences among compilation units are defined by with clauses;  that  is,
  147. a  compilation  unit  that mentions other library units in its with clauses
  148. depends on those library units.  These dependences between units are  taken
  149. into account for the determination of the allowed order of compilation (and
  150. recompilation)  of compilation units, as explained in section 10.3, and for
  151. the determination of the allowed order of elaboration of compilation units,
  152. as explained in section 10.5.
  153.  
  154. Notes:
  155.  
  156.  
  157. A library unit named by a with clause of  a  compilation  unit  is  visible
  158. (except  where hidden) within the compilation unit and hence can be used as
  159. a corresponding program unit.  Thus within the compilation unit,  the  name
  160. of  a  library  package can be given in use clauses and can be used to form
  161. expanded names;  a library subprogram can be called;  and  instances  of  a
  162. library generic unit can be declared.
  163.  
  164.  
  165. The  rules given for with clauses are such that the same effect is obtained
  166. whether the name of a library unit is mentioned once or more than  once  by
  167. the applicable with clauses, or even within a given with clause.
  168.  
  169. Example 1 : A main program:
  170.  
  171.  
  172. The  following  is  an  example  of  a  main program consisting of a single
  173. compilation unit:  a procedure for printing the real roots of  a  quadratic
  174. equation.   The  predefined  package  TEXT_IO  and  a  user-defined package
  175. REAL_OPERATIONS (containing the definition of the  type  REAL  and  of  the
  176. packages  REAL_IO  and REAL_FUNCTIONS) are assumed to be already present in
  177. the program library.  Such packages may be used by other main programs.
  178.  
  179.  
  180.     with TEXT_IO, REAL_OPERATIONS; use REAL_OPERATIONS;
  181.     procedure QUADRATIC_EQUATION is
  182.        A, B, C, D : REAL;
  183.        use REAL_IO,         --  achieves direct visibility of GET and PUT for REAL
  184.            TEXT_IO,         --  achieves direct visibility of PUT for strings and of NEW_LINE
  185.            REAL_FUNCTIONS;  --  achieves direct visibility of SQRT
  186.     begin
  187.        GET(A); GET(B); GET(C);
  188.        D := B**2 - 4.0*A*C;
  189.        if D < 0.0 then
  190.           PUT("Imaginary Roots.");
  191.        else
  192.           PUT("Real Roots : X1 = ");
  193.           PUT((-B - SQRT(D))/(2.0*A)); PUT(" X2 = ");
  194.           PUT((-B + SQRT(D))/(2.0*A));
  195.        end if;
  196.        NEW_LINE;
  197.     end QUADRATIC_EQUATION;
  198.  
  199. Notes on the example:
  200.  
  201.  
  202. The with clauses of a compilation unit need only mention the names of those
  203. library subprograms and packages whose  visibility  is  actually  necessary
  204. within  the  unit.   They  need  not (and should not) mention other library
  205. units that are used in turn by some of the units named in the with clauses,
  206. unless these other library units are also  used  directly  by  the  current
  207. compilation unit.  For example, the body of the package REAL_OPERATIONS may
  208. need elementary operations provided by other packages.  The latter packages
  209. should  not  be  named by the with clause of QUADRATIC_EQUATION since these
  210. elementary operations are not directly called within its body.
  211.  
  212.  
  213. References:  allow 1.6,  compilation  unit  10.1,  direct  visibility  8.3,
  214. elaboration 3.9, generic body 12.2, generic unit 12.1, hiding 8.3, instance
  215. 12.3,  library unit 10.1, main program 10.1, must 1.6, name 4.1, package 7,
  216. package body 7.1, package declaration 7.1, procedure 6.1, program  unit  6,
  217. secondary  unit  10.1,  simple  name  4.1, standard predefined package 8.6,
  218. subprogram body 6.3, subprogram declaration 6.1, subunit  10.2,  type  3.3,
  219. use clause 8.4, visibility 8.3
  220.  
  221. > 10.1.2  Examples of Compilation Units                     
  222.  
  223.  
  224. A  compilation  unit  can be split into a number of compilation units.  For
  225. example, consider the following program.
  226.  
  227.  
  228.     procedure PROCESSOR is
  229.  
  230.        SMALL : constant := 20;
  231.        TOTAL : INTEGER  := 0;
  232.  
  233.        package STOCK is
  234.           LIMIT : constant := 1000;
  235.           TABLE : array (1 .. LIMIT) of INTEGER;
  236.           procedure RESTART;
  237.        end STOCK;
  238.  
  239.        package body STOCK is
  240.           procedure RESTART is
  241.           begin
  242.              for N in 1 .. LIMIT loop
  243.                 TABLE(N) := N;
  244.              end loop;
  245.           end;
  246.        begin
  247.           RESTART;
  248.        end STOCK;
  249.  
  250.        procedure UPDATE(X : INTEGER) is
  251.           use STOCK;
  252.        begin
  253.           ...
  254.           TABLE(X) := TABLE(X) + SMALL;
  255.           ...
  256.        end UPDATE;
  257.  
  258.     begin
  259.        ...
  260.        STOCK.RESTART;  -- reinitializes TABLE
  261.        ...
  262.     end PROCESSOR;
  263.  
  264.  
  265. The following three compilation units  define  a  program  with  an  effect
  266. equivalent to the above example (the broken lines between compilation units
  267. serve  to remind the reader that these units need not be contiguous texts).
  268.  
  269.  
  270. Example 2 : Several compilation units:
  271.  
  272.  
  273.     package STOCK is
  274.        LIMIT : constant := 1000;
  275.        TABLE : array (1 .. LIMIT) of INTEGER;
  276.        procedure RESTART;
  277.     end STOCK;
  278.  
  279.     -------------------------------------------------
  280.  
  281.  
  282.     package body STOCK is
  283.        procedure RESTART is
  284.        begin
  285.           for N in 1 .. LIMIT loop
  286.              TABLE(N) := N;
  287.           end loop;
  288.        end;
  289.     begin
  290.        RESTART;
  291.     end STOCK;
  292.  
  293.     -------------------------------------------------
  294.  
  295.  
  296.     with STOCK;
  297.     procedure PROCESSOR is
  298.  
  299.        SMALL : constant := 20;
  300.        TOTAL : INTEGER  := 0;
  301.  
  302.        procedure UPDATE(X : INTEGER) is
  303.           use STOCK;
  304.        begin
  305.           ...
  306.           TABLE(X) := TABLE(X) + SMALL;
  307.           ...
  308.        end UPDATE;
  309.     begin
  310.        ...
  311.        STOCK.RESTART;  --  reinitializes TABLE
  312.        ...
  313.     end PROCESSOR;
  314.  
  315.  
  316. Note that in the latter version, the package STOCK  has  no  visibility  of
  317. outer  identifiers  other  than  the predefined identifiers (of the package
  318. STANDARD).  In particular, STOCK does not use any  identifier  declared  in
  319. PROCESSOR  such  as  SMALL  or  TOTAL;  otherwise STOCK could not have been
  320. extracted from PROCESSOR in the above manner.  The procedure PROCESSOR,  on
  321. the  other  hand,  depends  on  STOCK  and  mentions this package in a with
  322. clause.  This permits the inner occurrences of STOCK in the  expanded  name
  323. STOCK.RESTART and in the use clause.
  324.  
  325.  
  326. These three compilation units can be submitted in one or more compilations.
  327. For  example,  it  is  possible to submit the package specification and the
  328. package body together and in this order in a single compilation.
  329.  
  330.  
  331. References:   compilation  unit  10.1,  declaration  3.1,  identifier  2.3,
  332. package  7,  package  body  7.1,  package  specification  7.1,  program 10,
  333. standard package 8.6, use clause 8.4, visibility 8.3, with clause 10.1.1
  334.  
  335. > 10.2  Subunits of Compilation Units
  336.  
  337.  
  338. A subunit is used for the separate compilation of  the  proper  body  of  a
  339. program  unit  declared  within  another  compilation unit.  This method of
  340. splitting a program permits hierarchical program development.
  341.  
  342.  
  343.     body_stub ::=
  344.          subprogram_specification is separate;
  345.        | package body package_simple_name is separate;
  346.        | task body task_simple_name is separate;
  347.  
  348.     subunit ::=
  349.          separate (parent_unit_name) proper_body
  350.  
  351.  
  352. A body stub is only allowed as the body of a program unit (a subprogram,  a
  353. package,  a  task  unit,  or  a  generic  unit)  if  the  body  stub occurs
  354. immediately within either the specification of a  library  package  or  the
  355. declarative part of another compilation unit.
  356.  
  357.  
  358. If the body of a program unit is a body stub, a separately compiled subunit
  359. containing  the  corresponding  proper  body is required.  In the case of a
  360. subprogram, the subprogram specifications given in the proper body  and  in
  361. the body stub must conform (see 6.3.1).
  362.  
  363.  
  364. Each subunit mentions the name of its parent unit, that is, the compilation
  365. unit  where  the corresponding body stub is given.  If the parent unit is a
  366. library unit, it is called the ancestor library unit.  If the  parent  unit
  367. is  itself  a  subunit,  the  parent  unit name must be given in full as an
  368. expanded name, starting with the simple name of the ancestor library  unit.
  369. The  simple  names of all subunits that have the same ancestor library unit
  370. must be distinct identifiers.
  371.  
  372.  
  373. Visibility within the proper body of a subunit is the visibility that would
  374. be obtained at the place of the corresponding body stub (within the  parent
  375. unit)  if  the with clauses and use clauses of the subunit were appended to
  376. the context clause of the parent unit.  If the  parent  unit  is  itself  a
  377. subunit,  then  the  same  rule is used to define the visibility within the
  378. proper body of the parent unit.
  379.  
  380.  
  381. The effect of the elaboration of a body stub is  to  elaborate  the  proper
  382. body of the subunit.
  383.  
  384. Notes:
  385.  
  386.  
  387. Two  subunits  of  different library units in the same program library need
  388. not have distinct identifiers.  In any case, their full expanded names  are
  389. distinct,  since  the  simple names of library units are distinct and since
  390. the simple names of all subunits that have a given library unit as ancestor
  391. unit are also distinct.  By  means  of  renaming  declarations,  overloaded
  392. subprogram names that rename (distinct) subunits can be introduced.
  393.  
  394.  
  395. A  library unit that is named by the with clause of a subunit can be hidden
  396. by a declaration (with the same identifier) given in the proper body of the
  397. subunit.  Moreover, such a library unit can even be hidden by a declaration
  398. given within a parent unit since a library unit  acts  as  if  declared  in
  399. STANDARD;   this  however  does  not  affect the interpretation of the with
  400. clauses themselves, since only names of library units can  appear  in  with
  401. clauses.
  402.  
  403.  
  404. References:   compilation  unit 10.1, conform 6.3.1, context clause 10.1.1,
  405. declaration 3.1, declarative part 3.9, direct visibility  8.3,  elaboration
  406. 3.9,  expanded  name  4.1.3,  generic  body  12.2,  generic unit 12, hidden
  407. declaration 8.3, identifier 2.3, library unit 10.1, local declaration  8.1,
  408. name 4.1, occur immediately within 8.1, overloading 8.3, package 7, package
  409. body  7.1,  package  specification  7.1, program 10, program unit 6, proper
  410. body 3.9, renaming declaration 8.5, separate compilation 10.1, simple  name
  411. 4.1,  subprogram 6, subprogram body 6.3, subprogram specification 6.1, task
  412. 9, task body 9.1, task unit 9.1,  use  clause  8.4,  visibility  8.3,  with
  413. clause 10.1.1
  414.  
  415. > 10.2.1  Examples of Subunits
  416.  
  417.  
  418. The  procedure TOP is first written as a compilation unit without subunits.
  419.  
  420.  
  421.     with TEXT_IO;
  422.     procedure TOP is
  423.  
  424.        type REAL is digits 10;
  425.        R, S : REAL := 1.0;
  426.  
  427.        package FACILITY is
  428.           PI : constant := 3.14159_26536;
  429.           function  F(X : REAL) return REAL;
  430.           procedure G(Y, Z : REAL);
  431.        end FACILITY;
  432.  
  433.        package body FACILITY is
  434.           --  some local declarations followed by
  435.  
  436.           function F(X : REAL) return REAL is
  437.           begin
  438.              --  sequence of statements of F
  439.              ...
  440.           end F;
  441.  
  442.           procedure G(Y, Z : REAL) is
  443.              --  local procedures using TEXT_IO
  444.              ...
  445.           begin
  446.              --  sequence of statements of G
  447.              ...
  448.           end G;
  449.        end FACILITY;
  450.  
  451.        procedure TRANSFORM(U : in out REAL) is
  452.           use FACILITY;
  453.        begin
  454.           U := F(U);
  455.           ...
  456.        end TRANSFORM;
  457.     begin -- TOP
  458.        TRANSFORM(R);
  459.        ...
  460.        FACILITY.G(R, S);
  461.     end TOP;
  462.  
  463.  
  464. The body of the package FACILITY and that of the procedure TRANSFORM can be
  465. made into separate subunits of TOP.  Similarly, the body of the procedure G
  466. can be made into a subunit of FACILITY as follows.
  467.  
  468.  
  469. Example 3:
  470.  
  471.  
  472.     procedure TOP is
  473.  
  474.        type REAL is digits 10;
  475.        R, S : REAL := 1.0;
  476.  
  477.        package FACILITY is
  478.           PI : constant := 3.14159_26536;
  479.           function  F(X : REAL) return REAL;
  480.           procedure G(Y, Z : REAL);
  481.        end FACILITY;
  482.  
  483.        package body FACILITY is separate;                 --  stub of FACILITY
  484.        procedure TRANSFORM(U : in out REAL) is separate;  --  stub of TRANSFORM
  485.  
  486.     begin  --  TOP
  487.        TRANSFORM(R);
  488.        ...
  489.        FACILITY.G(R, S);
  490.     end TOP;
  491.  
  492.     -------------------------------------------------
  493.  
  494.  
  495.     separate (TOP)
  496.     procedure TRANSFORM(U : in out REAL) is
  497.        use FACILITY;
  498.     begin
  499.        U := F(U);
  500.        ...
  501.     end TRANSFORM;
  502.  
  503.     -------------------------------------------------
  504.  
  505.  
  506.     separate (TOP)
  507.     package body FACILITY is
  508.        --  some local declarations followed by
  509.  
  510.        function F(X : REAL) return REAL is
  511.        begin
  512.           --  sequence of statements of F
  513.           ...
  514.        end F;
  515.  
  516.        procedure G(Y, Z : REAL) is separate;              -- stub of G
  517.     end FACILITY;
  518.  
  519.     -------------------------------------------------
  520.  
  521.  
  522.     with TEXT_IO;
  523.     separate (TOP.FACILITY)  --  full name of FACILITY
  524.     procedure G(Y, Z : REAL) is
  525.        --  local procedures using TEXT_IO
  526.        ...
  527.     begin
  528.        --  sequence of statements of G
  529.        ...
  530.     end G;
  531.  
  532.  
  533. In the above example TRANSFORM and FACILITY are subunits of TOP, and G is a
  534. subunit of FACILITY.  The visibility in the split version is the same as in
  535. the initial version except for one change:   since  TEXT_IO  is  only  used
  536. within  G,  the  corresponding  with clause is written for G instead of for
  537. TOP.   Apart  from  this  change,  the  same  identifiers  are  visible  at
  538. corresponding  program points in the two versions.  For example, all of the
  539. following  are (directly) visible within the proper body of the subunit  G:
  540. the  procedure  TOP,  the  type  REAL,  the  variables R and S, the package
  541. FACILITY and the contained named number PI and subprograms F and G.
  542.  
  543.  
  544. References:  body stub 10.2, compilation unit 10.1, identifier  2.3,  local
  545. declaration  8.1,  named number 3.2, package 7, package body 7.1, procedure
  546. 6, procedure body 6.3, proper body 3.9, subprogram 6,  type  3.3,  variable
  547. 3.2.1, visibility 8.3, with clause 10.1.1
  548.  
  549. > 10.3  Order of Compilation
  550.  
  551.  
  552. The  rules  defining  the  order  in which units can be compiled are direct
  553. consequences of the visibility rules and, in particular, of the  fact  that
  554. any  library  unit that is mentioned by the context clause of a compilation
  555. unit is visible in the compilation unit.
  556.  
  557.  
  558. A compilation unit must be compiled after all library units  named  by  its
  559. context clause.  A secondary unit that is a subprogram or package body must
  560. be  compiled after the corresponding library unit.  Any subunit of a parent
  561. compilation unit must be compiled after the parent compilation unit.
  562.  
  563.  
  564. If any error is detected while attempting to compile  a  compilation  unit,
  565. then  the attempted compilation is rejected and it has no effect whatsoever
  566. on the program library;  the same holds for recompilations (no  compilation
  567. unit can become obsolete because of such a recompilation).
  568.  
  569.  
  570. The  order in which the compilation units of a program are compiled must be
  571. consistent with the partial ordering defined by the above rules.
  572.  
  573.  
  574. Similar rules apply for recompilations.  A compilation unit is  potentially
  575. affected  by  a  change in any library unit named by its context clause.  A
  576. secondary unit is potentially affected by a  change  in  the  corresponding
  577. library  unit.   The  subunits of a parent compilation unit are potentially
  578. affected by a change of the parent compilation unit.  If a compilation unit
  579. is successfully recompiled, the compilation units potentially  affected  by
  580. this  change  are obsolete and must be recompiled unless they are no longer
  581. needed.  An implementation may be able to reduce the compilation  costs  if
  582. it  can deduce that some of the potentially affected units are not actually
  583. affected by the change.
  584.  
  585.  
  586. The subunits of a unit can be recompiled without affecting the unit itself.
  587. Similarly, changes in a subprogram or package  body  do  not  affect  other
  588. compilation  units  (apart  from  the  subunits  of  the  body) since these
  589. compilation  units  only  have  access  to  the   subprogram   or   package
  590. specification.  An implementation is only allowed to deviate from this rule
  591. for  inline inclusions, for certain compiler optimizations, and for certain
  592. implementations of generic program units, as described below.
  593.  
  594.  
  595.   -  If a pragma INLINE is applied to a subprogram declaration given  in  a
  596.      package  specification,  inline inclusion will only be achieved if the
  597.      package body is compiled before units calling the subprogram.  In such
  598.      a case, inline inclusion creates a dependence of the calling  unit  on
  599.      the  package  body,  and  the compiler  must recognize this dependence
  600.      when deciding on the need for recompilation.  If  a  calling  unit  is
  601.      compiled  before  the  package  body, the pragma may be ignored by the
  602.      compiler for such calls (a  warning  that  inline  inclusion  was  not
  603.      achieved may be issued).  Similar considerations apply to a separately
  604.      compiled subprogram for which an INLINE pragma is specified.
  605.  
  606.  
  607.   -  For optimization purposes, an implementation may compile several units
  608.      of a given compilation in a way that creates further dependences among
  609.      these   compilation   units.    The  compiler  must  then  take  these
  610.      dependences into account when deciding on the need for recompilations.
  611.  
  612.  
  613.   -  An implementation may require  that  a  generic  declaration  and  the
  614.      corresponding proper body be part of the same compilation, whether the
  615.      generic  unit  is  itself  separately  compiled or is local to another
  616.      compilation unit.  An implementation may also require that subunits of
  617.      a generic unit be part of the same compilation.
  618.  
  619.  
  620. Examples of Compilation Order:
  621.  
  622.  
  623. (a)  In example 1 (see 10.1.1):  The procedure QUADRATIC_EQUATION  must  be
  624.      compiled after the library packages TEXT_IO  and REAL_OPERATIONS since
  625.      they appear in its with clause.
  626.  
  627.  
  628. (b)  In example 2 (see 10.1.2):  The package body STOCK  must  be  compiled
  629.      after the corresponding package specification.
  630.  
  631.  
  632. (c)  In example 2 (see 10.1.2):  The specification  of  the  package  STOCK
  633.      must  be  compiled before the procedure PROCESSOR.  On the other hand,
  634.      the procedure PROCESSOR can be compiled either  before  or  after  the
  635.      package body STOCK.
  636.  
  637.  
  638. (d)  In example 3 (see 10.2.1):  The procedure G must be compiled after the
  639.      package  TEXT_IO  since this package is named by the with clause of G.
  640.      On the other hand, TEXT_IO can be compiled either before or after TOP.
  641.  
  642.  
  643. (e)  In example 3 (see 10.2.1):  The subunits TRANSFORM and  FACILITY  must
  644.      be compiled after the main program TOP.  Similarly, the subunit G must
  645.      be compiled after its parent unit FACILITY.
  646.  
  647. Notes:
  648.  
  649.  
  650. For  library  packages,  it  follows  from  the  recompilation rules that a
  651. package body is made obsolete by the  recompilation  of  the  corresponding
  652. specification.   If  the  new  package specification is such that a package
  653. body is not required (that  is,  if  the  package  specification  does  not
  654. contain  the  declaration  of  a program unit), then the recompilation of a
  655. body for this package is not required.  In any case, the  obsolete  package
  656. body  must  not  be  used  and  can  therefore  be deleted from the program
  657. library.
  658.  
  659.  
  660. References:   compilation  10.1,  compilation  unit  10.1,  context  clause
  661. 10.1.1,  elaboration  3.9,  generic  body  12.2,  generic declaration 12.1,
  662. generic unit 12, library  unit  10.1,  local  declaration  8.1,  name  4.1,
  663. package  7,  package body 7.1, package specification 7.1, parent unit 10.2,
  664. pragma inline 6.3.2, procedure 6.1, procedure body 6.3,  proper  body  3.9,
  665. secondary  unit  10.1,  subprogram  body  6.3,  subprogram declaration 6.1,
  666. subprogram specification 6.1,  subunit  10.2,  type  3.3,  variable  3.2.1,
  667. visibility 8.3, with clause 10.1.1
  668.  
  669. > 10.4  The Program Library
  670.  
  671.  
  672. Compilers are required to enforce the language rules in the same manner for
  673. a  program  consisting of several compilation units (and subunits) as for a
  674. program submitted as a single compilation.  Consequently,  a  library  file
  675. containing information on the compilation units of the program library must
  676. be  maintained  by the compiler or compiling environment.  This information
  677. may include symbol tables and other information  pertaining to the order of
  678. previous compilations.
  679.  
  680.  
  681. A normal submission to the compiler consists of the compilation unit(s) and
  682. the library file.  The latter is used for checks and is  updated  for  each
  683. compilation unit successfully compiled.
  684.  
  685. Notes:
  686.  
  687.  
  688. A  single  program  library  is  implied  for  the  compilation  units of a
  689. compilation.  The possible existence of different program libraries and the
  690. means by which they are named are not concerns of the language  definition;
  691. they are concerns of the programming environment.
  692.  
  693.  
  694. There  should  be  commands  for  creating  the  program library of a given
  695. program or of a given family of programs.  These commands  may  permit  the
  696. reuse  of  units  of  other  program  libraries.   Finally, there should be
  697. commands for interrogating the status of the units of  a  program  library.
  698. The form of these commands is not specified by the language definition.
  699.  
  700.  
  701. References:   compilation  unit  10.1,  context  clause  10.1.1,  order  of
  702. compilation 10.3, program 10.1, program library  10.1,  subunit  10.2,  use
  703. clause 8.4, with clause 10.1.1
  704.  
  705. > 10.5  Elaboration of Library Units
  706.  
  707.  
  708. Before  the  execution  of  a main program, all library units needed by the
  709. main program are elaborated, as well  as  the  corresponding  library  unit
  710. bodies,  if  any.  The library units needed by the main program are:  those
  711. named by with clauses applicable to the main program, to its body,  and  to
  712. its  subunits;   those  named  by  with clauses applicable to these library
  713. units themselves, to the corresponding library unit bodies,  and  to  their
  714. subunits;  and so on, in a transitive manner.
  715.  
  716.  
  717. The  elaboration  of  these  library units and of the corresponding library
  718. unit bodies is performed in an order consistent with the  partial  ordering
  719. defined  by  the  with  clauses  (see  10.3).   In addition, a library unit
  720. mentioned by the context clause of a subunit must be elaborated before  the
  721. body of the ancestor library unit of the subunit.
  722.  
  723.  
  724. An  order of elaboration that is consistent with this partial ordering does
  725. not always ensure that each library unit  body  is  elaborated  before  any
  726. other compilation unit whose elaboration necessitates that the library unit
  727. body  be  already  elaborated.   If  the  prior elaboration of library unit
  728. bodies is needed, this can be requested by a pragma ELABORATE.  The form of
  729. this pragma is as follows:
  730.  
  731.     pragma ELABORATE (library_unit_simple_name {, library_unit_simple_name});
  732.  
  733.  
  734. These pragmas are only allowed immediately after the context  clause  of  a
  735. compilation  unit  (before  the subsequent library unit or secondary unit).
  736. Each argument of such a pragma must be the simple name of  a  library  unit
  737. mentioned  by the context clause, and this library unit must have a library
  738. unit body.  Such a pragma specifies that the  library  unit  body  must  be
  739. elaborated  before  the  given  compilation unit.  If the given compilation
  740. unit is a subunit, the library unit body must be elaborated before the body
  741. of the ancestor library unit of the subunit.
  742.  
  743.  
  744. The program is illegal if no consistent order can be found (that is,  if  a
  745. circularity  exists).   The  elaboration  of  the  compilation units of the
  746. program is performed in some order that is otherwise  not  defined  by  the
  747. language.
  748.  
  749.  
  750. References:   allow  1.6,  argument of a pragma 2.8, compilation unit 10.1,
  751. context  clause  10.1.1,  dependence  between   compilation   units   10.3,
  752. elaboration  3.9,  illegal  1.6, in some order 1.6, library unit 10.1, name
  753. 4.1,  main  program  10.1,  pragma  2.8,  secondary  unit  10.1,   separate
  754. compilation 10.1, simple name 4.1, subunit 10.2, with clause 10.1.1
  755.  
  756. > 10.6  Program Optimization
  757.  
  758.  
  759. Optimization  of  the  elaboration  of  declarations  and  the execution of
  760. statements may be performed by compilers.  In particular, a compiler may be
  761. able to optimize a program by evaluating certain expressions,  in  addition
  762. to  those  that  are  static expressions.  Should one of these expressions,
  763. whether static or not, be such that an exception would  be  raised  by  its
  764. evaluation,  then  the  code in that path of the program can be replaced by
  765. code to raise the exception;  the same holds for exceptions raised  by  the
  766. evaluation of names and simple expressions.  (See also section 11.6.)
  767.  
  768.  
  769. A  compiler  may  find  that  some  statements or subprograms will never be
  770. executed, for example, if their execution depends on a condition  known  to
  771. be FALSE.  The corresponding object machine code can then be omitted.  This
  772. rule permits the effect of conditional compilation within the language.
  773.  
  774. Note:
  775.  
  776.  
  777. An  expression  whose  evaluation  is  known to raise an exception need not
  778. represent an error if it occurs in a statement or subprogram that is  never
  779. executed.  The compiler may warn the programmer of a potential error.
  780.  
  781.  
  782. References:   condition  5.3,  declaration 3.1, elaboration 3.9, evaluation
  783. 4.5, exception 11, expression 4.4, false boolean value 3.5.3,  program  10,
  784. raising  of exceptions 11.3, statement 5, static expression 4.9, subprogram
  785. 6
  786.